package com.raja.servingStation.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.constant.Constants;
import com.raja.servingStation.common.constant.MysqlPageVo;
import com.raja.servingStation.common.util.RequestUtil;
import com.raja.servingStation.common.util.ResponseUtil;
import com.raja.servingStation.common.util.ReturnCodes;
import com.raja.servingStation.controller.common.BaseController;
import com.raja.servingStation.service.TbOrderService;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 待办工单
 */
@RestController
public class OrderController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private ResponseUtil responseUtil;
    @Autowired
    private TbOrderService orderService;
    /**
     * 统计待办总数
     * @param request
     * @return
     */
    @RequestMapping(value = "/daiban/daiBanCount",method = RequestMethod.GET)
    public ResponseEntity queryDaiBanCount(HttpServletRequest request){
        try {
            Integer servingStationId = RequestUtil.getUserId(request);
            int count = orderService.queryDaiBanOrderCount(servingStationId);
            return responseUtil.success(count,servingStationId);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return responseUtil.exception(ReturnCodes.REQUEST_EXCEPTION);
        }
    }

    /**
     * 待办列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/daiban/list",method = RequestMethod.GET)
    public ResponseEntity queryDaiBan(HttpServletRequest request){
        try {
            JSONObject body = RequestUtil.parseBody(request);
            Integer currPage = body.getInteger("currPage");
            Integer pageSize = body.getInteger("pageSize");
            Integer userId = RequestUtil.getUserId(request);
            if(currPage == null)
                return responseUtil.exception(ReturnCodes.ERR_param_ERROR);
            MysqlPageVo mysqlPageVo = new MysqlPageVo();
            if(pageSize==null)
                pageSize = Constants.pageSize;
            mysqlPageVo.setPageSize(pageSize,currPage);
            mysqlPageVo = orderService.queryListByPage(body,mysqlPageVo,userId);
            return responseUtil.success(mysqlPageVo);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return responseUtil.exception(ReturnCodes.REQUEST_EXCEPTION);
        }
    }

}
